package com.ahwei.shop.auth.security.handler;

import com.ahwei.r.R;
import com.ahwei.r.ResultCode;
import com.ahwei.shop.auth.util.ResponseUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ahWei
 * @email ahwei163@qq.com
 * @date 2022-05-23 16:03:21
 * @description 访问拒绝处理器、无权限！
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Resource
    private ResponseUtils responseUtils;


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 403 表示要求客户端认证
        responseUtils.outWriter(R.error(ResultCode.LOGIN_AUTH,"没有访问权限!"),HttpStatus.FORBIDDEN.value(),response);
    }
}
